<?php get_instance()->view('common/header');?>
<div class="page-container home">
    <?php get_instance()->view('common/sidebar');?>
    <div class="page-content-wrapper">
        <div class="page-content">
            <div class="row">
                <div class="col-md-12">
                    <!-- BEGIN PAGE TITLE & BREADCRUMB-->
                    <h3 class="page-title"> <?php if($is_add):?>新增区域 <?php else:?>编辑区域<?php endif;?> </h3>
                    <ul class="page-breadcrumb breadcrumb">
                        <li class="btn-group"> <a class="btn default ml_5 return" href="<?php echo getUrl('setting/shipping/area?id='.get('shipping_id'));?>"><i class="fa fa-chevron-left"></i> 返回</a> </li>
                        <li> <i class="fa fa-home"></i> <a href="<?php echo getUrl('home');?>"> 首页  <i class="fa fa-angle-right"></i> </a></li>
                        <li><a href="<?php echo getUrl('setting/shipping/area?id='.get('shipping_id'));?>">区域列表  <i class="fa fa-angle-right"></i></a></li>
                        <li><a href="javascript:void(0);"><?php if($is_add):?>新增 <?php else:?>编辑<?php endif;?></a></li>
                    </ul>
                    <!-- END PAGE TITLE & BREADCRUMB-->
                </div>
            </div>
            <!--开始-->

            <div class="row">
                <div class="col-md-12">
                    <!-- BEGIN VALIDATION STATES-->
                    <div class="portlet box">
                        <div class="portlet-body form">
                            <!-- BEGIN FORM-->
                            <form action="" class="form-horizontal" method="post" novalidate="novalidate" id="main-form">
                                <input type="hidden" name="shipping_id" value="<?php echo $is_add ?  get('shipping_id'):$shipping['shipping_id'];?>">
                                <div class="form-body">
                                    <div class="form-group">
                                        <label class="control-label col-md-3">区域名称 <span class="required">*</span></label>
                                        <div class="col-md-6">
                                            <input maxlength="12" class="form-control required" name="shipping_area_name" value="<?php echo  $is_add ? '' : $shipping['shipping_area_name'];?>" type="text">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3">首重1KG以内费用 <span class="required">*</span></label>
                                        <div class="col-md-6">
                                            <input maxlength="20" class="form-control required" name="money" value="<?php echo  $is_add ? '' : $shipping['money'];?>" type="text">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3">续重每1KG或其零数的费用 <span class="required">*</span></label>
                                        <div class="col-md-6">
                                            <input maxlength="20" class="form-control required" name="add_money" value="<?php echo  $is_add ? '' : $shipping['add_money'];?>" type="text">
                                        </div>
                                    </div>
                                    <div class="form-group">
		                                <label class="control-label col-md-3">区域： </label>
		                                <div class="col-md-8" id="area_list">
                                            <?php foreach ($select_area as $area):  ?>
                                            <input class="area_list" type="checkbox" checked name="area_list[]" value="<?php echo $area['id']?>"><?php echo $area['name']?>
                                            <?php endforeach;?>

                                        </div>
		                           </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3">配送区域名称： </label>
                                        <div class="col-md-8">
                                            <select name="province" id="province" size="10">
                                                <option value="0">请选择省份</option>
                                                <?php foreach ($province as $p):  ?>
                                                    <option value="<?php echo $p['id']?>"><?php echo $p['name']?></option>
                                                <?php endforeach;?>
                                            </select>
                                            <select name="city" id="city" size="10">
                                                <option value="0">请选择城市</option>
                                            </select>
                                            <select name="county" id="county" size="10">
                                                <option value="0">请选择</option>
                                            </select>
                                            <button id="addArea" class="btn btn-info" type="button">
                                                <i class="ace-icon fa fa-plus bigger-110"></i>
                                            </button>
                                        </div>
                                    </div>

                                </div>
                                <div class="form-actionsc fluid">
                                    <div class="col-md-offset-3 col-md-9">
                                    	<input type="hidden" name="id" value="<?php echo  $is_add ? '' : $shipping['id'];?>">
                                        <button type="submit" class="btn green"><i class="fa fa-save"></i> 保存</button>
                                        <a href="<?php echo getUrl('setting/shipping/area?id='.get('shipping_id'));?>" class="btn default"><i class="fa fa-chevron-left"></i> 返回</a>
                                    </div>
                                </div>
                            </form>

                            <!-- END FORM-->
                        </div>

                    </div>
                    <!-- END VALIDATION STATES-->
                </div>
            </div>

            <!--结束-->
        </div>
    </div>
</div>
<script>
    $(function(){
        $.e_validate.init('#main-form', {});
    });
    //  添加配送区域
    $('#addArea').click(function(){

        //
        var province = $("#province").val(); // 省份
        var city = $("#city").val();        // 城市
        var county = $("#county").val(); // 县镇
        var text = '';  // 中文文本
        var tpl = ''; // 输入框 html
        var is_set = 0; // 是否已经设置了

        // 设置 县镇
        if(county > 0){
            text = $("#county").find('option:selected').text();
            tpl = '<input class="area_list" type="checkbox" checked name="area_list[]" value="'+county+'">'+text;
            is_set = county; // 街道设置了不再设置市
        }
        // 如果县镇没设置 就获取城市
        if(is_set == 0 && city > 0){
            text = $("#city").find('option:selected').text();
            tpl = '<input class="area_list" type="checkbox" checked name="area_list[]" value="'+city+'">'+text;
            is_set = city;  // 市区设置了不再设省份

        }
        // 如果城市没设置  就获取省份
        if(is_set == 0 && province > 0){
            text = $("#province").find('option:selected').text();
            tpl = '<input class="area_list" type="checkbox" checked name="area_list[]" value="'+province+'">'+text;
            is_set = province;

        }

        var obj = $("input[class='area_list']"); // 已经设置好的复选框拿出来
        var exist = 0;  // 表示下拉框选择的 是否已经存在于复选框中
        $(obj).each(function(){
            if($(this).val() == is_set){  //当前下拉框的如果已经存在于 复选框 中
                show_message({status:0, message:'已经存在该区域'});
                exist = 1; // 标识已经存在
            }
        })

        if(!exist){
           $('#area_list').append(tpl); // 不存在就追加进 去

        }

    });
    //选择省份
    $('#province').change(function(){
        var province=$("#province").val();
        city(province);
    });

    //选择城市
    $('#city').change(function(){
        var city=$("#city").val();
        county(city);
    });

    //省市联动
    function city(province){
        l_blockUI();
        if(province != ''){
            var url= "<?php echo getUrl('member/consumer/ajaxChangeCheck');?>";
            $.post(url,{parent_id:province},function(re){
                $("#city").find("option").remove();
                var city = "<option value=''>选择城市</option>";
                $.each( re, function(i,item){
                    city += "<option value='" + item['id'] + "'>" + item['name'] + "</option>";
                });
                $("#city").html(city);

                App.unblockUI();
            },'json')
            $("#county").html("<option>选择区县</option>");
        }
    }
    //市县联动
    function county(city){
        l_blockUI();
        if(city!=''){
            var url= "<?php echo getUrl('member/consumer/ajaxChangeCheck');?>";
            $.post(url,{parent_id:city},function(re){
                $("#county").find("option").remove();
                var county = "<option>选择区县</option>";
                $.each( re, function(i,item){
                    county += "<option value='" + item['id'] + "'>" + item['name'] + "</option>";
                });
                $("#county").html(county);
                App.unblockUI();
            },'json')
        }
    }
</script>
</body>
</html>